using MediatR;
using RBAC_Class.Domain;
using RBAC_Class.ErrorCode;
using RBAC_Class.Infrastructrue.Interfaces;
using RBAC_Class.Read.API.Application.Command.Roles;
using RBAC_Class.Read.API.Dto;
using AutoMapper;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Security;
using System;
using NLog;

namespace RBAC_Class.Read.API.Application.CommandHandler.Roles
{
    public class GetAllRoleCommandHandler : IRequestHandler<GetAllRoleCommand, APIResult<List<Role>>>
    {
        private readonly IBaseRepository<Role> roleRep;
        private readonly ILogger<GetAllRoleCommandHandler> logger;

        public GetAllRoleCommandHandler(IBaseRepository<Role> roleRep, ILogger<GetAllRoleCommandHandler> logger)
        {
            this.roleRep = roleRep;
            this.logger = logger;
        }

        public Task<APIResult<List<Role>>> Handle(GetAllRoleCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = roleRep.GetAll();
                return Task.FromResult(new APIResult<List<Role>>()
                {
                    Code = ResultCode.Success,
                    Data = list.ToList(),
                    Message = "查询成功"
                });
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取所有角色失败");
                throw;
            }
        }
    }
} 